package cc.mrbird.unit.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.FileUtil;
import cc.mrbird.system.domain.User;
import cc.mrbird.unit.domain.ContactInfor;
import cc.mrbird.unit.domain.UnitInfo;
import cc.mrbird.unit.service.ContactInforService;
import cc.mrbird.unit.service.UnitInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Api(description = "联络信息接口")
@RestController
public class ContactInforController extends BaseController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ContactInforService contactInforService;

    @Log("获取联络信息")
    @RequestMapping(value = "contactInfor",method = RequestMethod.POST)
    /* @RequiresPermissions("contactInfor:list")*/
    public String index() {
        return "unit/contactInfor/contactInfor";
    }

    @Log("获取联络信息")
    @ApiOperation(value = "获取联络信息" ,  notes="获取联络信息")
    @RequestMapping(value = "contactInfor/list",method = RequestMethod.POST)
    /*@RequiresPermissions("contactInfor:list")*/
    @ResponseBody
    public Map<String, Object> ContactInforList(QueryRequest request, ContactInfor contactInfor) {
        return super.selectByPageNumSize(request, () -> this.contactInforService.findContactInforWithRole(contactInfor, request));
    }

    @ApiOperation(value = "导出Excel")
    @RequestMapping(value = "contactInfor/excel",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo ContactInforExcel(ContactInfor contactInfor){
        try {
            List<ContactInfor> list = this.contactInforService.findContactInforWithRole(contactInfor, null);
            return FileUtil.createExcelByPOIKit("联络信息表", list, ContactInfor.class);
        } catch (Exception e) {
            log.error("导出联络信息表Excel失败", e);
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

    @ApiOperation(value = "导出csv")
    @RequestMapping(value = "contactInfor/csv",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo ContactInforCsv(ContactInfor contactInfor) {
        try {
            List<ContactInfor> list = this.contactInforService.findContactInforWithRole(contactInfor, null);
            return FileUtil.createCsv("联络信息表", list, ContactInfor.class);
        } catch (Exception e) {
            log.error("导出联络信息表Csv失败", e);
            return ResponseBo.error("导出Csv失败，请联系网站管理员！");
        }
    }

    @ApiOperation(value = "根据id查询联络信息" ,  notes="根据id查询联络信息")
    @RequestMapping(value = "contactInfor/getContactInfor",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo getContactInfor(Long unit_id) {
        try {
            ContactInfor contactInfor = this.contactInforService.findById(unit_id);
            return ResponseBo.ok(contactInfor);
        } catch (Exception e) {
            log.error("获取联络信息失败", e);
            return ResponseBo.error("获取联络信息失败，请联系网站管理员！");
        }
    }

    @ApiOperation(value = "联络信息添加")
    @RequestMapping(value = "contactInfor/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo add(ContactInfor contactInfor) {
        try {
            ContactInfor result = this.contactInforService.findByName(contactInfor.getContact_name());
            if (result != null) {
                return ResponseBo.warn("该联络信息已存在！");
            }
            User currentUser = super.getCurrentUser();
            this.contactInforService.add(contactInfor,currentUser);
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }
    @ApiOperation(value = "修改联络信息信息")
    @Log("修改联络信息信息")
    /* @RequiresPermissions("contactInfor:update")*/
    @RequestMapping(value = "contactInfor/update" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo update(ContactInfor contactInfor) {
        try {
            this.contactInforService.update(contactInfor);
            return ResponseBo.ok("修改联络信息信息成功！");
        } catch (Exception e) {
            log.error("修改联络信息信息失败", e);
            return ResponseBo.error("修改联络信息失败，请联系网站管理员！");
        }
    }
    @Log("删除联络信息")
    /*@RequiresPermissions("contactInfor:delete")*/
    @RequestMapping(value = "contactInfor/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo delete(String ids) {
        try {
            this.contactInforService.delete(ids);
            return ResponseBo.ok("删除联络信息成功！");
        } catch (Exception e) {
            log.error("删除联络信息失败", e);
            return ResponseBo.error("删除联络信息失败，请联系网站管理员！");
        }
    }

}
